<!--
  -- 描述：根据用户角色、用户单位获取用户列表
  --
-->
<template>
  <el-select placeholder="请选择" :value="mval" @change="selectHandler" :clearable=true :multiple="multiple" filterable>
    <el-option v-for="(item,index) in list" :key="index" :label="item.name" :value="item.username"></el-option>
  </el-select>
</template>

<script>
  export default {
    name: "DictionarySelect",
    model: {
      prop: 'mval',
      event: 'modelValue'
    },
    props: ['mval', 'multiple', 'roleCode', 'orgCode'],
    watch: {
      roleCode() {
        if (this.roleCode) {
          this.query();
        }
      },
      orgCode() {
        if (this.orgCode) {
          this.query();
        }
      }
    },
    mounted: function () {
      this.query();
    },
    data() {
      return {
        list: [],
        oldRoleCode: '',
        userId: this.$store.getters.userId,//用户ID
      }
    },
    methods: {
      query() {
        this.api.get('/bucket-system/system/user/getByRoleAndOrg?roleCode=' + this.roleCode + '&orgCode=' + this.orgCode, {})
          .then((data) => {
            this.list = data.data;
            // 过滤自身数据
            data.data.forEach((item, index) => {
              if (item.id === this.userId) {
                this.list.splice(index, 1);
              }
            });
          });
      },
      selectHandler: function (val) {
        this.$emit('modelValue', val)
      }
    }
  }
</script>

